package com.yc.damaimaven.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.yc.damaimaven.entity.Product;
import com.yc.damaimaven.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xw
 * @since 2024-07-09
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductMapper productMapper;
    @RequestMapping("queryIndexProduct")
    public Map<String,Object> queryIndexProduct(){
        //分页
        Page<Product> page=new PageDTO<>(1,10);
        //排序
        page.addOrder(OrderItem.desc("pid"));
        //查询最新的10个商品
        Page<Product> news=productMapper.selectPage(page,null);

        //重新构建新的分页和排序
        page=new PageDTO<>(1,10);
        page.addOrder(OrderItem.asc("pid"));
        //构建条件对象
        LambdaQueryWrapper<Product> lwq=new LambdaQueryWrapper<>();
        //is_hot=1 热门商品条件
        Page<Product> hots =productMapper.selectPage(page,lwq);
        //添加到结果集合中
        Map<String,Object> ret=new HashMap<>();
        ret.put("news",news);
        ret.put("hots",hots);
        return ret;

    }

    @RequestMapping("findById")
    public Product findById(int pid){
        return productMapper.selectById(pid);
    }

    @RequestMapping("queryByCsid")
    public Page<Product> queryByCsid(int csid,int size,int page){
        LambdaQueryWrapper<Product>lqw=new LambdaQueryWrapper<>();
        lqw.eq(Product::getCsid,csid);
        Page<Product>pageObj = new PageDTO<>(page,size);
        Page<Product> productPage = productMapper.selectPage(pageObj, lqw);
        return productPage;
    }
    @RequestMapping("queryByCid")
    public Page<Product> queryByCid(int cid,int size,int page){
        LambdaQueryWrapper<Product>lqw=new LambdaQueryWrapper<>();
        lqw.inSql(Product::getCsid,"select csid from categorysecond where cid="+cid);
        Page<Product>pageObj = new PageDTO<>(page,size);
        Page<Product> productPage = productMapper.selectPage(pageObj, lqw);
        return productPage;
    }

}
